package com.jpcc.vw.onlineRadio.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.application.MusicApplication;
import com.jpcc.vw.onlineRadio.model.bean.TopListAlbumData;
import com.jpcc.vw.onlineRadio.utils.ViewHolderUtils;

import java.util.ArrayList;
import java.util.List;

public class TopRanksListAlbumAdapter extends BaseAdapter {

    private List<TopListAlbumData> sources = new ArrayList<>();
    private Context context;

    public TopRanksListAlbumAdapter(Context c, List<TopListAlbumData> datas) {
        if (c != null) {
            context = c;
        }
        sources.clear();
        if (datas != null) {
            sources.addAll(datas);
        }
    }

    @Override
    public int getCount() {
        return sources.size();
    }

    @Override
    public TopListAlbumData getItem(int position) {
        return sources.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_album_category, null);
        }
        initViews(position, convertView);
        return convertView;
    }

    private void initViews(int position, View convertView) {
        TextView tv_position = ViewHolderUtils.get(convertView, R.id.tv_position);
        TextView tv_category = ViewHolderUtils.get(convertView, R.id.tv_category);
        ImageView imv_cover=ViewHolderUtils.get(convertView,R.id.imv_cover);
        tv_category.setText(sources.get(position).getTitle());
        tv_position.setText(position + 1 + "");
        Glide.with(MusicApplication.GetInstance()).load(sources.get(position).getCoverLarge()).into(imv_cover);
    }

    public void setData(List<TopListAlbumData> datas) {
        sources.clear();
        if (datas != null) {
            sources.clear();
            sources.addAll(datas);
            notifyDataSetChanged();
        }
    }
}
